import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from "@/stores"
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import("@/views/login/Login.vue")
    },
    {
      path: '/',
      component: () => import("@/layout/index.vue"),
      name: "layout",
      redirect: "/channel",
      children: [
        {
          path: '/channel',
          name: 'channel',
          component: () => import("@/views/channel/Channel.vue")
        },
        {
          path: '/sensitive',
          name: 'sensitive',
          component: () => import("@/views/sensitive/Sensitive.vue")
        },
        {
          path: '/audit',
          name: 'audit',
          component: () => import("@/views/auditUser/AuditUser.vue")
        },
        {
          path: '/audit-detail',
          name: 'audit-detail',
          component: () => import("@/views/auditUser/UserDetail.vue")
        },
      ]
    },
  ]
})

router.beforeEach((to,from, next) => {
  const store = useUserStore()
  if (!store.user?.token) {
    if(to.path === "/login") {
      next()
    }else {
      next("/login")
    }
  }else {
    next()
  }
})

export default router
